package com.towker.core.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.util.StringUtil;

public class StrUtil
{
	/**
	 *检测字符中是否全是数字型
	 * @param str
	 * @return
	 */
	public static boolean checked_str(String str)
	{
		Pattern pattern = Pattern.compile("[0-9]+");
		Matcher matcher = pattern.matcher((CharSequence) str);
		boolean result = matcher.matches();
		if (result)
		{
			return true;
		}
		return false;
	}

	/**
	 * 数字的转换（仅支持一百以内的数字）
	 * @param numf
	 * @param i
	 * @return
	 */
	public static String changeToUppNum(String numf)
	{
		String numUpper = "";
		String numExp = numf;
		if ("".equals(numExp))
		{
			return "";
		}
		String[] arrUppNum =
		{
				"十", "一", "二", "三", "四", "五", "六", "七", "八", "九"
		};
		for (int i = 0; i < numExp.length(); i++)
		{
			if ("1".equals(numExp.substring(i, i + 1)) && "0".equals(numExp.substring(i + 1, i + 2)))
			{
				numUpper = arrUppNum[0];
				break;
			}
			else if (numExp.length() > 1 && "1".equals(numExp.substring(i, i + 1)) && !"0".equals(numExp.substring(i + 1, i + 2)))
			{
				numUpper += arrUppNum[0];
				int num = Integer.parseInt(numExp.substring(i + 1, i + 2));
				switch (num)
				{
					case 1:
						numUpper += arrUppNum[1];
						break;
					case 2:
						numUpper += arrUppNum[2];
						break;
					case 3:
						numUpper += arrUppNum[3];
						break;
					case 4:
						numUpper += arrUppNum[4];
						break;
					case 5:
						numUpper += arrUppNum[5];
						break;
					case 6:
						numUpper += arrUppNum[6];
						break;
					case 7:
						numUpper += arrUppNum[7];
						break;
					case 8:
						numUpper += arrUppNum[8];
						break;
					case 9:
						numUpper += arrUppNum[9];
						break;
					case 0:
						numUpper += arrUppNum[0];
						break;
				}
				break;
			}
			else
			{
				int num = Integer.parseInt(numExp.substring(i, i + 1));
				switch (num)
				{
					case 1:
						numUpper += arrUppNum[1];
						break;
					case 2:
						numUpper += arrUppNum[2];
						break;
					case 3:
						numUpper += arrUppNum[3];
						break;
					case 4:
						numUpper += arrUppNum[4];
						break;
					case 5:
						numUpper += arrUppNum[5];
						break;
					case 6:
						numUpper += arrUppNum[6];
						break;
					case 7:
						numUpper += arrUppNum[7];
						break;
					case 8:
						numUpper += arrUppNum[8];
						break;
					case 9:
						numUpper += arrUppNum[9];
						break;
					case 0:
						numUpper += arrUppNum[0];
						break;
				}
				if (i < numExp.length() - 1 && !"0".equals(numExp.substring(i + 1, i + 2)))
				{
					numUpper += arrUppNum[0];
				}
			}
		}
		return numUpper;
	}

	/**
	 * 编号的转换
	 * @param numf
	 * @param i
	 * @return
	 */
	public static String changeToLowNum(String numf)
	{
		String numUpper = "";
		String numExp = numf;
		if ("".equals(numExp))
		{
			return "";
		}
		String[] arrUppNum =
		{
				"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"
		};
		for (int i = 0; i < numExp.length(); i++)
		{
			if (numExp.length() == 1)
			{
				if ('十' == numExp.charAt(i))
				{
					numUpper = arrUppNum[1] + arrUppNum[0];
				}
				else
				{
					char num = numExp.charAt(i);
					switch (num)
					{
						case '一':
							numUpper += arrUppNum[1];
							break;
						case '二':
							numUpper += arrUppNum[2];
							break;
						case '三':
							numUpper += arrUppNum[3];
							break;
						case '四':
							numUpper += arrUppNum[4];
							break;
						case '五':
							numUpper += arrUppNum[5];
							break;
						case '六':
							numUpper += arrUppNum[6];
							break;
						case '七':
							numUpper += arrUppNum[7];
							break;
						case '八':
							numUpper += arrUppNum[8];
							break;
						case '九':
							numUpper += arrUppNum[9];
							break;
						case '十':
							numUpper += arrUppNum[0];
							break;
					}
				}
			}
			else if (numExp.length() == 2)
			{
				if ('十' == numExp.charAt(0))
				{
					numUpper = arrUppNum[1];
					char num = numExp.charAt(i + 1);
					switch (num)
					{
						case '一':
							numUpper += arrUppNum[1];
							break;
						case '二':
							numUpper += arrUppNum[2];
							break;
						case '三':
							numUpper += arrUppNum[3];
							break;
						case '四':
							numUpper += arrUppNum[4];
							break;
						case '五':
							numUpper += arrUppNum[5];
							break;
						case '六':
							numUpper += arrUppNum[6];
							break;
						case '七':
							numUpper += arrUppNum[7];
							break;
						case '八':
							numUpper += arrUppNum[8];
							break;
						case '九':
							numUpper += arrUppNum[9];
							break;
						case '十':
							numUpper += arrUppNum[0];
							break;
					}
					break;
				}
				else
				{
					char num = numExp.charAt(i);
					switch (num)
					{
						case '一':
							numUpper += arrUppNum[1];
							break;
						case '二':
							numUpper += arrUppNum[2];
							break;
						case '三':
							numUpper += arrUppNum[3];
							break;
						case '四':
							numUpper += arrUppNum[4];
							break;
						case '五':
							numUpper += arrUppNum[5];
							break;
						case '六':
							numUpper += arrUppNum[6];
							break;
						case '七':
							numUpper += arrUppNum[7];
							break;
						case '八':
							numUpper += arrUppNum[8];
							break;
						case '九':
							numUpper += arrUppNum[9];
							break;
						case '十':
							numUpper += arrUppNum[0];
							break;
					}
				}
			}
		}
		return numUpper;
	}

	public static String replaceStr(String str, String problemStr, String replace)
	{
		for (int i = str.lastIndexOf(problemStr); i >= 0; i = str.lastIndexOf(problemStr, i - 1))
		{
			if (i == 0)
			{
				str = replace + str.substring(i + 1, str.length());
			}
			else
			{
				str = str.substring(0, i) + replace + str.substring(i + 1, str.length());
			}
		}
		return str;
	}

	public String replStr(String str)
	{
		if (str == null || str.length() == 0)
			return "";
		// 必须先转化&
		// return str.replaceAll("&", "&amp;").replaceAll("", "&nbsp;").replaceAll("<", "&lt;").replaceAll(">",
		// "&gt;").replaceAll("\"", "&quot;").replaceAll("'", "&apos;");
		return str.replaceAll("&amp;", "&").replaceAll("&nbsp;", "").replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&quot;", "\"").replaceAll("'", "&apos;");
	}

	public static void main(String[] args)
	{
		int[] m =
		{
				0, 1, 9, 13, 27, 39, 88, 100
		};
		int[] n = sort(m);
		for (int i = 0; i < m.length; i++)
		{
			System.out.println(n[i] + "\n");
		}
	}

	/* 冒泡排序算法 */
	public static int[] sort(int[] m)
	{
		int theLenth = m.length;
		/* 执行theLenth次 */
		for (int i = 0; i < theLenth; i++)
		{
			/* 每执行一次，将最小的数排在后面 */
			for (int j = 0; j < theLenth - i - 1; j++)
			{
				int a = m[j];
				int b = m[j + 1];
				if (a < b)
				{
					m[j] = b;
					m[j + 1] = a;
				}
			}
		}
		return m;
	}
}
